package cn.tedu.csmall.stock.webapi.kafka;

import cn.tedu.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.csmall.commons.pojo.stock.entity.Stock;
import cn.tedu.csmall.stock.service.IStockService;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;

//@Component
public class Producer {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Autowired
    private IStockService StockService;

    @Scheduled(fixedRate = 20000)
    public void SendMessage() {
        Stock stock =new Stock();
        stock.setId(1);
        stock.setCommodityCode("PU201");
        stock.setReduceCount(3);
        StockReduceCountDTO stockReduceCountDTO=new StockReduceCountDTO();
        BeanUtils.copyProperties(stock,stockReduceCountDTO);
        StockService.reduceCommodityCount(stockReduceCountDTO);

        Gson gson = new Gson();
        String json = gson.toJson(stock);

        System.out.println("减少库存3个成功");
        kafkaTemplate.send("myStock",json);


    }

}
